<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">储位编码</label>
        <el-input v-model.trim="query.storageCode" clearable placeholder="储位编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">储位类型</label>
        <el-select v-model="query.storageType" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.storage_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">占用情况</label>
        <el-input v-model.trim="query.takeStatus" clearable placeholder="占用情况" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <!-- <el-select v-model="query.takeStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.storage_takeStatus"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select> -->
        <label class="el-form-item-label">锁定情况</label>
        <el-select v-model="query.lockStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.storage_lockStatus"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">排</label>
        <el-select v-model.trim="query.xPaths" class="filter-item" collapse-tags multiple clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in xList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <label class="el-form-item-label">列</label>
        <el-select v-model.trim="query.yPaths" class="filter-item" collapse-tags multiple clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in yList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <label class="el-form-item-label">层</label>
        <el-select v-model.trim="query.zPaths" class="filter-item" collapse-tags multiple clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in zList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button type="primary" class="filter-item" size="mini" @click="batchUpadteButton">批量更新储位类型</el-button>
        </template>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="储位编码" prop="storageCode">
            <el-input v-model="form.storageCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="储位类型" prop="storageType">
            <el-select v-model="form.storageType" filterable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.storage_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="库区类型" prop="areaType">
            <el-input v-model="form.areaType" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="占用情况" prop="takeStatus">
            <!-- <el-input v-model="form.takeStatus" style="width: 250px;" /> -->
            <!-- <el-select v-model="form.takeStatus" class="filter-item" placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.storage_takeStatus"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select> -->
            <el-input v-model="form.takeStatus" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="锁定情况" prop="lockStatus">
            <!-- <el-input v-model="form.lockStatus" style="width: 250px;" /> -->
            <el-select v-model="form.lockStatus" class="filter-item" placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.storage_lockStatus"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="排" prop="xPath">
            <el-input v-model="form.xPath" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="列" prop="yPath">
            <el-input v-model="form.yPath" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="层" prop="zPath">
            <el-input v-model="form.zPath" style="width: 250px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="storageCode" label="储位编码" />
        <el-table-column prop="storageType" label="储位类型">
          <template slot-scope="scope">
            {{ dict.label.storage_type[scope.row.storageType] }}
          </template>
        </el-table-column>
        <el-table-column prop="areaType" label="库区类型" />
        <el-table-column prop="takeStatus" label="占用情况">
          <template slot-scope="scope">
            {{ scope.row.takeStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="lockStatus" label="锁定情况">
          <template slot-scope="scope">
            {{ dict.label.storage_lockStatus[scope.row.lockStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="xPath" label="排" />
        <el-table-column prop="yPath" label="列" />
        <el-table-column prop="zPath" label="层" />
        <!-- <el-table-column prop="location" label="（X，Y）" /> -->
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <el-dialog
        title="批量更新储位类型"
        :visible.sync="dialogFormVisible"
        width="500px"
        :close-on-click-modal="false"
      >
        <el-form ref="form" :model="batchForm" size="small" label-width="120px">
          <el-form-item label="储位类型" prop="storageType">
            <el-select v-model="batchForm.storageType" filterable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in dict.storage_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelUpdate">取 消</el-button>
          <el-button type="primary" :loading="btn.batchUpdate" :disabled="!batchForm.storageType" @click="confirmUpdate">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudStorageLocation from '@/api/work/storageManagement'
import { getqueryListAPI, confirmUpdateAPI } from '@/api/work/storageManagement'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, storageCode: null, storageType: null, areaType: null, createTime: null, updateTime: null, takeStatus: null, lockStatus: null, xPath: null, yPath: null, zPath: null, location: null, finishTime: null }
export default {
  name: 'StorageManagement',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['storage_takeStatus', 'storage_lockStatus', 'storage_type'],
  cruds() {
    return CRUD({ title: 'storageLocation', url: 'api/storageLocation', idField: 'id', sort: 'id,desc', crudMethod: { ...crudStorageLocation },
      optShow: {
        download: true,
        add: false,
        edit: false,
        del: false,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'storageLocation:add', 'otherUser'],
        edit: ['admin', 'storageLocation:edit', 'otherUser'],
        del: ['storageLocation:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'storageCode', display_name: '储位编码' },
        { key: 'storageType', display_name: '储位类型' }
      ],
      xList: [],
      yList: [],
      zList: [],
      dialogFormVisible: false,
      batchForm: {},
      btn: {
        batchUpdate: false
      }
    }
  },
  mounted() {
    this.getqueryList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    batchUpadteButton() {
      this.dialogFormVisible = true
    },
    cancelUpdate() {
      this.dialogFormVisible = false
      this.batchForm = {}
    },
    async confirmUpdate() {
      const submitForm = {
        xPaths: this.query.xPaths,
        yPaths: this.query.yPaths,
        zPaths: this.query.zPaths,
        storageType: this.batchForm.storageType
      }
      // console.log(submitForm)
      try {
        this.btn.batchUpdate = true
        const res = await confirmUpdateAPI(submitForm)
        if (res.result === 'OK') {
          this.$message.success('批量修改储位类型成功')
          this.dialogFormVisible = false
          this.batchForm = {}
          this.crud.toQuery()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.batchUpdate = false
      }
    },
    async getqueryList() {
      const res = await getqueryListAPI()
      if (res.result === 'OK') {
        // TODO: 获取List
        this.xList = res.data.xPathList
        this.yList = res.data.yPathList
        this.zList = res.data.zPathList
      }
    }
  }
}
</script>

<style scoped>

</style>
